--------------------------------------------------------------------------------
--  *  Prog name constant_bandwidth_server.ads
--  *  Project name CBS
--  *
--  *  Version 1.0
--  *  Last update 09/09/01
--  *
--  *  Created by Antonio Barros on 09/09/01.
--  *  Copyright (c) 2009 CISTER.
--  *  GNAT modified GNU General Public License
--  *


-- CONSTANT BANDWIDTH SERVER ---------------------------------------------------
--  <description>
--    This package provides the functionality of the Constant Bandwidth Server
--    [Abeni 1998].
--  </description>
--  <group>Group1</group>
--  <c_version>1.0.0</c_version>

with System;
with Ada.Real_Time; use Ada.Real_Time;
with Ada.Execution_Time.Group_Budgets; use Ada.Execution_Time.Group_Budgets;
with Ada.Task_Identification;

package Constant_Bandwidth_Servers is

  protected type Constant_Bandwidth_Server(Budget, Period : Positive) is

    pragma Interrupt_Priority(System.Interrupt_Priority'Last);

    procedure Register;
    --	<summary>Adds a task to the server.</summary>
    --  <exception>TEM DE LEVANTAR NO CASO DE FALHAR!!!!</exception>

    procedure Release;
    --	<summary>Requests the server to release a job that is ready.</summary>
    --  <exception>TEM DE LEVANTAR NO CASO DE FALHAR!!!!</exception>
    --  <seealso>Finish</seealso>

    procedure Finish;
    --  <summary>Job informs the server that has just finished executing. It
    --  is absolutely mandatory or else the server will hang.</summary>
    --  <exception>No exception</exception>
    --  <seealso>Release</seealso>

  private
    procedure Budget_Exausted(G : in out Group_Budget);
    --  <summary>This routine handles the server when it is exhausted.</summary>
    --  <parameter name="G">Group Budget associated to this server</parameter>
    --  <exception>No exception</exception>

    First : Boolean := true;
    Server_Budget : Group_Budget;
    Qs : Time_Span := Milliseconds(Budget);	-- Maximum budget.
    Ts : Time_Span := Milliseconds(Period);	-- Replenish period.
    Deadline : Time := Ada.Real_Time.Clock;  -- It can not be ZERO.
    Current_Job : Ada.Task_Identification.Task_Id;
    Pending_Jobs : Natural := 0;
    Idle : Boolean := true;
    -- CRIAR UM ARRAY DE TASK_ID PARA TER UM BUFFER CIRCULAR DE TAREFAS
    -- SUSPENSAS. CRIAR TAMBEM UMA CONSTANTE COM O NUMERO DE SLOTS.

  end Constant_Bandwidth_Server;

end Constant_Bandwidth_Servers;
